package com.xyy.saas.payment.merchant.core.dto;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.xyy.saas.payment.common.Constants;
import com.xyy.saas.payment.util.AmountCastUtil;
import lombok.Builder;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.Date;
import java.util.List;

@Data
@Builder
public class RechargePayDto {
    public static final String PAY_CHANNEL_WX = "0001";
    public static final String PAY_CHANNEL_ALIPAY = "0002";
    public static final String PAY_CHANNEL_JD = "0003";

    /**
     * 业务线
     */
    private String businessIdType;
    /**
     * 支付平台商户标识
     */
    private String accountId;
    /**
     * 支付渠道
     * RechargePayDto#PAY_CHANNEL_WX
     * RechargePayDto#PAY_CHANNEL_ALIPAY
     */
    private String channel;
    /**
     * 支付渠道商户号
     */
    private String channelMerchantNo;
    /**
     * 交易日期
     */
    private Date tradeDate;
    /**
     * 渠道支付单号（微信/支付宝订单号）
     */
    private String channelTradeNo;
    /**
     * 交易金额
     */
    private Integer tradeAmount;
    /**
     * 充值订单明细
     */
    private List<RechargePayOrderItem> orderItems;

    /**
     * 组装参数
     */
    public JSONArray assembleOrderItems() {
        if(CollectionUtils.isEmpty(orderItems)) {
            return null;
        }

        JSONArray result = new JSONArray();

        for(RechargePayOrderItem item : orderItems) {
            JSONObject obj = new JSONObject();
            obj.put("SubOrderFillSubAcctNo", item.getThirdUserId());
            obj.put("SubOrderFillAmt", AmountCastUtil.castFen2Yuan(item.getAmount()));
            obj.put("SubOrderFillCommission", NumberUtils.INTEGER_ZERO);
            obj.put("SubFillOrderNo", item.getPayNo());
            obj.put("SubOrderFillRemark", item.getRemark());
            result.add(obj);
        }

        return result;
    }

    /**
     * 渠道转换 支付平台->平安充值
     */
    public static String convertChannel(String paymentChannel) {
        if(StringUtils.isBlank(paymentChannel)) {
            return StringUtils.EMPTY;
        }

        if (paymentChannel.startsWith(Constants.CHANNEL_JDAGGREGATE_NAME)) {
            return PAY_CHANNEL_JD;
        }
        else if (Constants.CHANNEL_JD_CARD_NAME.equals(paymentChannel)) {
            return PAY_CHANNEL_JD;
        }
        else if(paymentChannel.contains(Constants.CHANNEL_WX_NAME)) {
            return PAY_CHANNEL_WX;
        }
        else if(paymentChannel.contains(Constants.CHANNEL_ALI_NAME)) {
            return PAY_CHANNEL_ALIPAY;
        }

        throw new UnsupportedOperationException(paymentChannel);
    }

    /**
     * 构建详情
     */
    public void buildDetail(String payNo, String thirdUserId, Integer amount, String remark) {
        setOrderItems(Lists.newArrayList(RechargePayOrderItem.builder().
                payNo(payNo).
                thirdUserId(thirdUserId).
                amount(amount).
                remark(remark).
                build()));
    }
}

/**
 * 充值明细
 */
@Data
@Builder
class RechargePayOrderItem {
    /**
     * 子订单号
     */
    private String payNo;
    /**
     * 充值账户
     */
    private String thirdUserId;
    /**
     * 充值金额
     */
    private Integer amount;
    /**
     * 备注
     */
    private String remark;
}
